package com.define.ssh.action.config;

import java.util.Date;
import java.util.HashMap;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.Restrictions;

import com.define.ssh.action.admin.BaseAdminAction;
import com.define.ssh.model.SysConfig;
import com.define.ssh.service.SysConfigService;
import com.define.ssh.util.InterfaceIpData;
import com.sxit.ecwx.core.utils.Formatu;

/**
 *  
 * 
 */
@ParentPackage("admin")
public class ParameterConfigAction extends BaseAdminAction {

	@Resource(name = "sysConfigServiceImpl")
	private SysConfigService sysConfigService;

	/** 页面传递参数过来 */
	protected int pageSize;
	protected int pageNum;
	
	private SysConfig sysConfig;

	public String index() {
		final HashMap<String, Object> searchMap = new HashMap<String, Object>();
		pager.setSearchMap(searchMap);
		pager = this.sysConfigService.findPager(pager,Restrictions.in("updateType", new Object[]{1,2}));
		
		return "parameterConfig";
	}

	public String save() {
		Date updatetime = sysConfigService.getUpdateTime("tb_sys_config");
		sysConfig.setUpdateTime(new Date());
		sysConfig.setUpdateType(1);
		this.sysConfigService.save(sysConfig);
		Long modfiytime = updatetime.getTime() / 1000;
	    byte[] by = new byte[4+4+2];
        System.arraycopy(Formatu.int2byte(InterfaceIpData.compid), 0, by,0, 4); /*操作用户 */
        System.out.println("InterfaceIpData.compid=="+InterfaceIpData.compid);
		System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,4, 4); /*操作时间  */
		short opertype=1;
		System.arraycopy(Formatu.short2byte(opertype), 0, by,8, 2); /*操作类型 */
		sysConfigService.sendMsg(by, 1001);
		
		return ajax(Status.success, "保存成功");
	}

	public String edit() {
		SysConfig temp=this.sysConfigService.get(sysConfig.getId());
		return ajax(temp);
	}

	public String update() {
		Date updatetime = sysConfigService.getUpdateTime("tb_sys_config");
		SysConfig temp=this.sysConfigService.get(sysConfig.getId());
		temp.setName(sysConfig.getName());
		temp.setContent(sysConfig.getContent());
		temp.setUpdateType(2);
		temp.setUpdateTime(new Date());
		this.sysConfigService.update(temp);
		Long modfiytime = updatetime.getTime() / 1000;
	    byte[] by = new byte[4+4+2];
        System.arraycopy(Formatu.int2byte(InterfaceIpData.compid), 0, by,0, 4); /*操作用户 */
		System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,4, 4); /*操作时间  */
		short opertype=2;
		System.arraycopy(Formatu.short2byte(opertype), 0, by,8, 2); /*操作类型 */
		sysConfigService.sendMsg(by, 1001);
		  System.out.println("InterfaceIpData.compid=="+InterfaceIpData.compid);
		return ajax(Status.success, "修改成功");
	}

	public String delete() {
		
		Date updatetime = sysConfigService.getUpdateTime("tb_sys_config");
		SysConfig temp=this.sysConfigService.get(sysConfig.getId());
		temp.setUpdateType(3);
		temp.setUpdateTime(new Date());
		this.sysConfigService.update(temp);
		Long modfiytime = updatetime.getTime() / 1000;
	    byte[] by = new byte[4+4+2];
        System.arraycopy(Formatu.int2byte(InterfaceIpData.compid), 0, by,0, 4); /*操作用户 */
		System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,4, 4); /*操作时间  */
		short opertype=3;
		System.arraycopy(Formatu.short2byte(opertype), 0, by,8, 2); /*操作类型 */
		sysConfigService.sendMsg(by, 1002);
		return ajax(Status.success, "删除成功");
	}

	public SysConfigService getSysConfigService() {
		return sysConfigService;
	}

	public void setSysConfigService(SysConfigService sysConfigService) {
		this.sysConfigService = sysConfigService;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public SysConfig getSysConfig() {
		return sysConfig;
	}

	public void setSysConfig(SysConfig sysConfig) {
		this.sysConfig = sysConfig;
	}
}